﻿using System.Data;
using UnityEngine;
using UnityEngine.UI;

namespace Framework.Utils
{
    public class UIUtil
    {
        public static float OffSetX = 0;
        public static float OffSetY = 0;

        public static RectTransform CreateModuleContainer(string moduleName, GameObject prefab, Transform parent)
        {
            GameObject container = new GameObject(moduleName);
            var rect = container.AddComponent<RectTransform>();
            rect.anchorMin = new Vector2(0, 0);
            rect.anchorMax = new Vector2(1, 1);
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = new Vector2(OffSetX, OffSetY);
            container.transform.SetParent(parent, false);

            Object.Instantiate(prefab, rect, false);

            return rect;
        }

        public static RectTransform CreateWindowMask(string windowName, GameObject prefab, Transform parent)
        {
            GameObject bgMask = new GameObject(windowName);

            RawImage image = bgMask.AddComponent<RawImage>();
            image.color = new Color(0, 0, 0, 0.5f);

            bgMask.transform.SetParent(parent, false);
            
            Object.Instantiate(prefab, bgMask.transform, false);
            
            var rect = image.rectTransform;
            rect.localPosition = new Vector3(0,0,0);
            rect.anchorMin = new Vector2(0, 0);
            rect.anchorMax = new Vector2(1, 1);
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
            
            return rect;
        }

        public static RectTransform CreateGuideMask(string guideName, RectTransform target, bool isStatic,
            Transform transform)
        {
            GameObject bgMask = new GameObject(guideName);

            HollowOutMask mask = bgMask.AddComponent<HollowOutMask>();
            mask.color = new Color(0,0,0,0.5f);
            bgMask.transform.SetParent(transform,false);
            
            var rect = mask.rectTransform;
            rect.localPosition = new Vector3(0,0,0);
            rect.anchorMin = new Vector2(0,0);
            rect.anchorMax = new Vector2(1,1);
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
            
            mask.SetTarget(target,isStatic);
            
            return rect;
        }
        
        public static RectTransform CreateWindowBg(string windowName, GameObject prefab, Transform transform)
        {
            GameObject bg = new GameObject(windowName);
            
            bg.AddComponent<Empty4Raycast>();
        
            bg.transform.SetParent(transform, false);
            
            Object.Instantiate(prefab, bg.transform, false);

            var rect = bg.GetComponent<RectTransform>();
            rect.localPosition = new Vector3(0,0,0);
            rect.anchorMin = new Vector2(0, 0);
            rect.anchorMax = new Vector2(1, 1);
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
            return rect;
        }
    }
}